
include board.mk
include config.mk

# Which compiler
ifeq ($(BOARD), ORANGEPIZERO)
CROSS = arm-linux-gnueabihf-
endif

export CC = $(CROSS)gcc
export AR = $(CROSS)ar
export AT = @


export PROJ_ROOT_PATH = $(shell pwd)
export MIDDLEWARE_PATH = $(PROJ_ROOT_PATH)/middleware
export DNA_PATH = $(PROJ_ROOT_PATH)/dna
export BUS_PATH = $(PROJ_ROOT_PATH)/bus
# export OBJS_PATH = $(PROJ_ROOT_PATH)/objs/
export BIN_PATH  = $(PROJ_ROOT_PATH)/bin/
export LIBS_PATH = $(PROJ_ROOT_PATH)/libs/
export APP = $(app)


SUBDIRS += $(PROJ_ROOT_PATH)/bus
SUBDIRS += $(PROJ_ROOT_PATH)/driver/userspace
SUBDIRS += $(PROJ_ROOT_PATH)/middleware/
# SUBDIRS += $(PROJ_ROOT_PATH)/dna


CFLAGS += -D$(OS_PLATFORM)


all: prepare all-libs app
	$(AT)echo "build done"

prepare:
	$(AT)echo "Compiler: $(CC)"
	$(AT)echo "Board: $(BOARD)"
	$(AT)echo "start building..."
	$(AT)mkdir -p $(BIN_PATH) $(LIBS_PATH)

all-libs:
	$(AT)echo "building all lib..."
	$(AT)for dir in $(SUBDIRS); \
	do \
		make -C $$dir;	\
	done
	$(AT)echo "build all libs done"

app:
	$(AT)echo "building app: $(APP)"
	$(AT)make -C apps/$(APP)
	$(AT)echo "build app done"


.PHONY: clean
clean: libs-clean apps-clean
	$(AT)-rm -rf $(BIN_PATH) $(LIBS_PATH)

libs-clean:
	$(AT)for dir in $(SUBDIRS); \
	do \
		make clean -C $$dir;	\
	done

apps-clean:
	$(AT)for dir in $(shell ls $(PROJ_ROOT_PATH)/apps); do   \
		if [ -d $(PROJ_ROOT_PATH)/apps/$$dir ]; then \
			make clean -C $(PROJ_ROOT_PATH)/apps/$$dir; \
		fi;   \
	done
